﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class TienComDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_TienCom> GetAllTienCom()
        {
            return DBDanhMuc.dm_TienComs.Where(p => p.IsDelete == false).ToList();
        }
        public dm_TienCom AddNewTienCom(dm_TienCom TienCom)
        {
            DBDanhMuc.dm_TienComs.InsertOnSubmit(TienCom);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteTienCom(int TienComID)
        {
            dm_TienCom TienComNew =
                DBDanhMuc.dm_TienComs.Single(p => p.Id == TienComID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_TienComs.DeleteOnSubmit(TienComNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteTienCom(dm_TienCom TienCom)
        {
            dm_TienCom TienComDelete =
                DBDanhMuc.dm_TienComs.Single(p => p.Id == TienCom.Id);
            if (TienComDelete.hs_NhanVien_PCComs.Count() == 0)
            {
                TienComDelete.Id = TienCom.Id;
                TienComDelete.UpdateDate = DateTime.Now;
                TienComDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                TienComDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdateTienCom(dm_TienCom TienCom)
        {
            dm_TienCom TienComUpdate =
                DBDanhMuc.dm_TienComs.Single(p => p.Id == TienCom.Id);

            TienComUpdate.Id = TienCom.Id;
            TienComUpdate.TienCom = TienCom.TienCom;
            TienComUpdate.UpdateDate = TienComUpdate.UpdateDate;
            TienComUpdate.UpdateBy = TienComUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_TienCom> SearchTienComPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_TienCom>)DBDanhMuc.dm_TienComs.Where(p => p.TienCom == decimal.Parse(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_TienCom GetTienComById(string id)
        {
            try
            {
                dm_TienCom item = this.DBDanhMuc.dm_TienComs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_TienCom GetTienComByTenCheck(decimal ten)
        {

            IEnumerable<dm_TienCom> list = this.DBDanhMuc.dm_TienComs.Where(p => p.TienCom == ten && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_TienCom GetTienComByTen(decimal ten)
        {

            IEnumerable<dm_TienCom> list = this.DBDanhMuc.dm_TienComs.Where(p => p.TienCom == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_TienCom> RecycleGetTienComPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TienComs.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_TienCom> GetTienComPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TienComs.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_TienCom BuildNewTienCom(TienComForm form)
        {
            dm_TienCom newTienCom = new dm_TienCom();
            if (!string.IsNullOrEmpty(form.TienC))
                newTienCom.TienCom = decimal.Parse(form.TienC.Replace(".", ""), null);
            newTienCom.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newTienCom.CreateDate = DateTime.Now;
            newTienCom.IsDelete = false;
            return newTienCom;
        }
        public int PhucHoiTienCom(int id)
        {
            dm_TienCom phuchoi = new dm_TienCom();
            phuchoi = this.GetTienComById(Convert.ToString(id));
            dm_TienCom item = new dm_TienCom();
            item = this.GetTienComByTenCheck(phuchoi.TienCom.Value);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_TienCom BuildEditTienCom(TienComForm form)
        {
            dm_TienCom newTienCom = new dm_TienCom();
            newTienCom.Id = form.id;
            if (!string.IsNullOrEmpty(form.TienC))
                newTienCom.TienCom = decimal.Parse(form.TienC.Replace(".", ""), null);
            newTienCom.UpdateDate = DateTime.Now;
            newTienCom.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newTienCom;
        }
        public dm_TienCom GetLoaiCongChucIdMax()
        {
            IList<dm_TienCom> list = DBDanhMuc.dm_TienComs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public TienComForm ConvertToForm(dm_TienCom obj)
        {
            TienComForm form = new TienComForm();
            form.id = obj.Id;
            form.TienC = obj.TienCom != null ? obj.TienCom.Value.ToString("#,#").Replace(",", ".") : "";
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}